//
//  CFPasswordView.m
//  CFfuturestalent
//
//  Created by 子yu 刘 on 2018/5/1.
//  Copyright © 2018年 子yu 刘. All rights reserved.
//

#import "CFPasswordView.h"
#import "CFpswItme.h"

@interface CFPasswordView()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (nonatomic ,strong)UITextField * tfPassword;
@property (nonatomic ,strong)UICollectionView * collectionView;
@property (nonatomic ,assign)NSInteger strCount;
@end

@implementation CFPasswordView

+(CFPasswordView *)sharedInstance{
    static CFPasswordView * view;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        view = [[CFPasswordView alloc] init];
        
    });
    return view;
}

-(instancetype)init{
    if (self = [super init]) {
        [self initView];
         [self.tfPassword becomeFirstResponder];
    }
    return self;
}

+ (void)showWithBtnClicked:(verifyPassWord)block{
    [[CFPasswordView sharedInstance] showVerifyPassWord:block];
}

- (void)showVerifyPassWord:(verifyPassWord )block{
    self.frame = CGRectMake(0, 0, DEF_SCREEN_WIDTH(), DEF_SCREEN_HEIGHT());
    [DEF_WINDOW addSubview:self];
    _block = block;
}

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self initView];
        [self.tfPassword becomeFirstResponder];
    }
    return self;
}

- (void)initView{
    self.tfPassword = [[UITextField alloc] init];
    self.tfPassword.keyboardType = UIKeyboardTypeNumberPad;
    MJWeakSelf;
    [[self.tfPassword rac_signalForControlEvents:UIControlEventEditingChanged] subscribeNext:^(__kindof UIControl * _Nullable x) {
        UITextField * tf = (UITextField *)x;
        if (tf.text.length>=6) {
            tf.text = [tf.text substringToIndex:6];
        }
        weakSelf.strCount = tf.text.length;
        [weakSelf.collectionView reloadData];
    }];

    [self addSubview:self.tfPassword];
    [self.tfPassword mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.mas_equalTo(self);
        make.width.height.mas_offset(0);
    }];
    self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
    UIView * bgView = [[UIView alloc] init];
    bgView.backgroundColor = [UIColor whiteColor];
    bgView.layer.masksToBounds = YES;
    bgView.layer.cornerRadius = 6;
    [self addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self);
        make.top.mas_equalTo(126*Bit);
        make.width.mas_equalTo(300);
        make.height.mas_equalTo(210);
    }];
    
    UIButton * cha = [UIButton buttonWithType:UIButtonTypeCustom];
    [cha setImage:[UIImage imageNamed:@"cha"] forState:UIControlStateNormal];
    [cha addTarget:self action:@selector(cancle) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:cha];
    [cha mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.mas_equalTo(0);
        make.width.right.mas_offset(38);
    }];
    
    UILabel * label = [[UILabel alloc] init];
    label.text = @"请输入六位数的资金密码";
    label.font = [UIFont systemFontOfSize:15];
    label.textColor = [UIColor colorWithHexString:@"000000"];
    label.textAlignment = NSTextAlignmentCenter;
    [bgView addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self);
        make.top.mas_equalTo(38);
    }];
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.sectionInset = UIEdgeInsetsMake(1, 1, 1, 1);
    layout.minimumLineSpacing = 1;
    layout.minimumInteritemSpacing = 1;
    UICollectionView * collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    collectionView.backgroundColor = [UIColor colorWithHexString:@"bfbfbf"];
    [bgView addSubview:collectionView];
    collectionView.scrollEnabled = NO;
    collectionView.delegate = self;
    collectionView.dataSource = self;
    [collectionView registerClass:[CFpswItme class] forCellWithReuseIdentifier:@"CFpswItme"];

    [collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self);
        make.width.mas_offset(247);
        make.height.mas_offset(42);
        make.top.mas_equalTo(label.mas_bottom).offset(28);
    }];
    self.collectionView = collectionView;
    
    UIButton * withdrawBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    withdrawBtn.backgroundColor = DEF_COLOR_RED;
    [withdrawBtn setTitle:@"立即提现" forState:UIControlStateNormal];
    withdrawBtn.layer.masksToBounds = YES;
    withdrawBtn.layer.cornerRadius = 6;
    [[withdrawBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        if (self.tfPassword.text.length<6) {
            [AlertUtil alertMessage:@"请输入六位密码"];
        }else{
            [self removeFromSuperview];
            if (self.block) {
                self.block();
            }
        }

    }];
    [bgView addSubview:withdrawBtn];
    [withdrawBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self);
        make.width.mas_offset(115);
        make.height.mas_offset(35);
        make.bottom.mas_equalTo(-23);
    }];
}

- (void)cancle{
    [self removeFromSuperview];
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 6;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * cellId = @"CFpswItme";
    CFpswItme * cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
    if (indexPath.row>=self.strCount) {
        cell.point.hidden = YES;
    }else{
        cell.point.hidden = NO;
    }
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    return CGSizeMake(40,40);
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    [self.tfPassword becomeFirstResponder];
}

@end
